package org.jxc.web.action;

import java.util.List;

import org.apache.struts2.ServletActionContext;
import org.jxc.domain.Product;
import org.jxc.domain.ProductWarehouseStock;
import org.jxc.domain.Warehouse;
import org.jxc.service.StockService;
import org.jxc.service.WarehouseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.team4.exception.CRUDException;
import org.team4.web.action.GenericAction;

import antlr.StringUtils;

import freemarker.template.utility.StringUtil;

@Controller
@Scope("prototype")
public class StockAction extends GenericAction<ProductWarehouseStock> {

	private static final long serialVersionUID = -8465525870762638814L;
	@Autowired
	private StockService stockService;
	
	@Autowired
	private WarehouseService warehouseService;
	private List<Warehouse> wlist;
	
	private ProductWarehouseStock object;
	
	public String listPagination() throws Exception {
		page = stockService.listPagination(page, searchType, searchValue);
		return SUCCESS;
	}
	
	public String choose() throws Exception {
		page = stockService.listPagination(page, searchType, searchValue);
		return SUCCESS;
	}
	
	public String chooseBack() throws CRUDException {
		page = stockService.listPaginationBack(page, searchType, searchValue);
		return SUCCESS;
	}
	
	public String toSave() throws CRUDException {
		wlist = warehouseService.getList(new Warehouse());
		return SUCCESS;
	}
	
	public String save() throws CRUDException {
		String[] amountsStr = ServletActionContext.getRequest().getParameterValues("amounts");
		if(idList != null && idList.size() >0 ){
			Product temp = null;
			for (int i = 0; i < idList.size(); i++) {
				temp = new Product();
				temp.setId(idList.get(i));
				object.setProduct(temp);
				object.setAmounts(Integer.parseInt(amountsStr[i]));
				stockService.save(object);
			}
		}
		return SUCCESS;
	}

	public String toUpdate() throws CRUDException {
		object = stockService.findById(object.getId());
		wlist = warehouseService.getList(new Warehouse());
		return SUCCESS;
	}
	
	public String update() throws CRUDException {
		stockService.update(object);
		return SUCCESS;
	}
	
	public ProductWarehouseStock getObject() {
		return object;
	}

	public void setObject(ProductWarehouseStock object) {
		this.object = object;
	}

	public List<Warehouse> getWlist() {
		return wlist;
	}

	public void setWlist(List<Warehouse> wlist) {
		this.wlist = wlist;
	}
}
